# 设置交叉编译前缀，例如 arm-none-eabi- 或 aarch64-none-elf- CROSS_COMPILE ?=
CROSS_COMPILE ?=

# 是否使用 pulseaudio，yes 表示使用，no 表示不使用
LINUX_PULSEAUDIO := no

# 是否使用 alsa，yes 表示使用，no 表示不使用
LINUX_ALSA := yes

 # 是否使用 OpenSL ES (Android)，yes 表示使用，no 表示不使用
ANDROID_OPENSLES := yes

# 设置调试级别，0 表示关闭调试，1-4 表示不同的调试级别
#DEBUG_LEVEL ?= 3

BUILD := build/
SRC := src/
INCLUDE := src/include
OUT = out/
TARGET := mp3-player

LIBMAD := src/libmad/
LIBMAD_SRC := $(LIBMAD)src/
LIBMAD_INCLUDE := $(LIBMAD)inc/

HEADSET := headset/
HEADSET_SRC := ${SRC}$(HEADSET)
HEADSET_INCLUDE := $(HEADSET_SRC)

PLAYER_VERSION ?= 0.0.1

AS := $(CROSS_COMPILE)as
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
OBJCOPY := $(CROSS_COMPILE)objcopy
OBJDUMP := $(CROSS_COMPILE)objdump
NASM := nasm

CFLAGS += -I${INCLUDE} -I${LIBMAD_INCLUDE} -I${HEADSET_INCLUDE}
ifneq ($(DEBUG_LEVEL),)
	CFLAGS += -DCONFIG_DEBUG_LEVEL=$(DEBUG_LEVEL)
endif
CFLAGS += -DPLAYER_VERSION='"$(PLAYER_VERSION) $(shell git describe --always --dirty)"'
CFLAGS += -DHAVE_CONFIG_H  # have config.h in libmad

SRCS := mp3-player.c list-files.c mp3-parse-id3v2.c mp3-info.c

ifeq ($(OS), Windows_NT)
	SRCS += pcm-player-win-mm.c
	SRCS += pcm-player-win-dsound.c
	SRCS += headset/headset-win.c
	LIBS += -ldsound -lwinmm -lsetupapi -lhid
else ifeq ($(shell uname), Linux)
	ifneq ($(LINUX_PULSEAUDIO), no)
		SRCS += pcm-player-linux-pulseaudio.c
		LIBS += -lpulse -lpulse-simple
	endif
	ifneq ($(LINUX_ALSA), no)
		SRCS += pcm-player-linux-alsa.c
		LIBS += -lasound
	endif
	ifeq ($(shell uname -o), Android)
		ifneq ($(ANDROID_OPENSLES), no)
			SRCS += pcm-player-android-opensles.c
			LIBS += -lOpenSLES
		endif
	endif
	SRCS += headset/headset-linux.c
	LIBS += -lpthread -lm
else ifeq ($(shell uname), Darwin)
	SRCS += pcm-player-macos.c
	SRCS += headset/headset-macos.c
	LIBS += -framework CoreAudio -framework AudioToolbox -framework AudioUnit -framework Carbon -framework IOKit
endif

OBJS := $(SRCS:.c=.o)
OBJS := $(addprefix $(BUILD), $(OBJS))
DEPS := $(SRCS:.c=.d)
DEPS := $(addprefix $(BUILD), $(DEPS))

LIBMAD_SRCS :=  bit.c decoder.c  fixed.c frame.c   huffman.c layer3.c   layer12.c   stream.c  synth.c   timer.c version.c
LIBMAD_OBJS = $(LIBMAD_SRCS:.c=.o)
LIBMAD_OBJS := $(addprefix $(BUILD)$(LIBMAD), $(LIBMAD_OBJS))

$(info SRCS = $(SRCS))
$(info LIBMAD_SRCS = $(LIBMAD_SRCS))


all: $(OUT)$(TARGET)

$(OUT)$(TARGET): $(OBJS) $(LIBMAD_OBJS)  Makefile | $(OUT)
	$(CC) $(CFLAGS) -g -O2 -o $@ $(OBJS) $(LIBMAD_OBJS)  $(LIBS)


# 生成依赖文件
$(BUILD)%.d: $(SRC)%.c | $(BUILD) ${BUILD}$(HEADSET)
	@set -e; rm -f $@; \
	$(CC) -MM $(CFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,$(BUILD)\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

# 包含依赖文件
-include $(DEPS)


$(BUILD)$(LIBMAD)%.o: $(LIBMAD_SRC)%.c | $(BUILD)$(LIBMAD)
	$(CC) $(CFLAGS) -c -O2 -g -o $@ $<

$(BUILD)$(HEADSET)%.o: $(HEADSET_SRC)%.c | ${BUILD}$(HEADSET)
	$(CC) $(CFLAGS) -c -O2 -g -o $@ $<

$(BUILD)%.o: $(SRC)%.c | $(BUILD)
	$(CC) $(CFLAGS) -c -g -O2 -o $@ $<

$(OUT) $(BUILD) $(BUILD)$(LIBMAD) $(BUILD)$(HEADSET):
	mkdir -p $@

clean:
	rm -rf $(BUILD) $(OUT)

run: $(OUT)$(TARGET)
	./$(OUT)$(TARGET) deer.mp3

run2: $(OUT)$(TARGET)
	./$(OUT)$(TARGET) sample.mp3

.PHONY: clean run run2
